﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SimpleMvvmToolkit;

namespace GUIEXAM13
{
    public class Events : ModelBase<Events>
    {
        private EventDataAccess _eventDataAccess;

        public Events()
        {
            _eventDataAccess = new EventDataAccess();
            LoadAllEvents();
        }

        private ObservableCollection<EventDomain> _allEvents;
        public ObservableCollection<EventDomain> AllEvents
        {
            get { return _allEvents = _allEvents ?? new ObservableCollection<EventDomain>(); }
            set
            {
                _allEvents = value;
                NotifyPropertyChanged(m => m.AllEvents);
            }
        }

        public void LoadAllEvents()
        {
            AllEvents = _eventDataAccess.GetAllEvents();
        }

        public void SaveAllEvents()
        {
            var bgWorker = new BackgroundWorker();
            bgWorker.DoWork += (sender, args) => _eventDataAccess.SaveAllEvents(AllEvents);
            bgWorker.RunWorkerAsync();
        }
    }
}
